﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cecbData;

namespace cecbClasses.Data_Adapters.MatrixSimple
{
    public class SimpleMatrixCalculation
    {

        public SimpleMatrixCalculation()
        {
            cecbContext = new DSSEntities();
          
        }
        DSSEntities cecbContext;


        double sum;


        // calculate the Physical Environmental Affection
        public void getPhysicalEnvironmentalAffection(string project, out Boolean isCorrect, out Double physicalValue)
        {
            double phisicalImpact = 0;


            sum = getSummation(project);

                var count1 = (from c in cecbContext.SimpleMatrixValues
                              join b in cecbContext.Impacts on c.impt_reference equals b.impt_reference
                              join g in cecbContext.Components on b.impt_component equals g.cmpt_reference
                              where c.proj_reference.Equals(project)
                              && g.cmpt_mainCat.Contains("Physical Environment") 

                              group c by new
                              {
                                  c.proj_reference
                              }

                                  into cc
                                  select cc.Sum(c => c.mtrx_value) / sum).FirstOrDefault();
             
                try
                {
                    phisicalImpact = count1.Value;
                    physicalValue = Math.Round(phisicalImpact, 2);
                    isCorrect = true;
                }
                catch (Exception)
                {
                    physicalValue = 0.00;
                    isCorrect = false;
                }

             
        }


        // calculate the Physical Environmental Affection as a presentage
        public void getPhysicalEnvironmentalAffectionPresentage(string project , out Boolean isCorrect , out Double physicalValue )
        {
            double presentage = 0;
            sum = getSummation(project);
            if (sum == 0) sum = 1;
            var quary = (from c in cecbContext.SimpleMatrixValues
                             join b in cecbContext.Impacts on c.impt_reference equals b.impt_reference
                             join g in cecbContext.Components on b.impt_component equals g.cmpt_reference
                             where c.proj_reference.Contains(project)
                             && g.cmpt_mainCat.Contains("Physical Environment")

                             group c by new
                             {
                                 c.proj_reference
                             }
                              
                             into cc
                             select cc.Sum(c => c.mtrx_value) / sum).FirstOrDefault();

                try
                {
                    presentage = quary.Value;
                    physicalValue = Math.Round(presentage, 2) * 100;
                    isCorrect = true;
                }
                catch (Exception)
                {
                    isCorrect = false;
                    physicalValue = 0.00;
                }
            
        }



        public void getEchologicallyAffectin(string project, out Boolean isCorrect, out Double returnlValue)
        {
            sum = getSummation(project);
            if (sum == 0) sum = 1;
            try
            {

                var quary = (from c in cecbContext.SimpleMatrixValues
                             join b in cecbContext.Impacts on c.impt_reference equals b.impt_reference
                             join g in cecbContext.Components on b.impt_component equals g.cmpt_reference
                             where c.proj_reference.Contains(project)
                             && g.cmpt_mainCat.Contains("Ecological Environmental")

                             group c by new
                             {
                                 c.proj_reference
                             }

                                 into cc
                                 select cc.Sum(c => c.mtrx_value) / sum).FirstOrDefault();

                
                    returnlValue = Math.Round(quary.Value, 2);
                    isCorrect = true;
                 
            }
            catch (Exception)
            {
                returnlValue = 0;
                isCorrect = false;
            }
             
        }
        // calculate the Echological Environmental Affection as a presentage
        public void getEchologicallyAffectinPresentage(string project, out Boolean isCorrect, out Double returnlValue)
        {
           
            double presentage = 0;
            sum = getSummation(project);
            if (sum == 0) sum = 1;
            try
            {
                var quary = (from c in cecbContext.SimpleMatrixValues
                             join b in cecbContext.Impacts on c.impt_reference equals b.impt_reference
                             join g in cecbContext.Components on b.impt_component equals g.cmpt_reference
                             where c.proj_reference.Contains(project)
                             && g.cmpt_mainCat.Contains("Ecological Environmental")

                             group c by new
                             {
                                 c.proj_reference
                             }

                                 into cc
                                 select cc.Sum(c => c.mtrx_value) / sum).FirstOrDefault();

                presentage = quary.Value;
                presentage = Math.Round(presentage, 2) * 100;
                returnlValue = presentage;
                isCorrect = true;
            }
            catch (Exception)
            {
                isCorrect = false;
                returnlValue = 0.0;
            }
           
             
        }
        // calculate the SociologicallyAffection as aPresentage
        public void getSociologicallyAffectionPresentage(string project, out Boolean isCorrect, out Double returnlValue)
        {
            double presentage = 0;
            sum = getSummation(project);
            if (sum == 0) sum = 1;
            try
            {
                var quary = (from c in cecbContext.SimpleMatrixValues
                             join b in cecbContext.Impacts on c.impt_reference equals b.impt_reference
                             join g in cecbContext.Components on b.impt_component equals g.cmpt_reference
                             where c.proj_reference.Contains(project)
                             && g.cmpt_mainCat.Contains("Social Environmental")

                             group c by new
                             {
                                 c.proj_reference
                             }

                                 into cc
                                 select cc.Sum(c => c.mtrx_value) / sum).FirstOrDefault();

                presentage = quary.Value;
                presentage = Math.Round(presentage, 2) * 100;
                returnlValue = presentage;
                isCorrect = true;
            }
            catch (Exception)
            {
                returnlValue = 0.0;
                isCorrect = false;
            }
            

            
        }

        // calculate the Sociological Environmental Affection 
        public void getSociologicallyAffection(string project, out Boolean isCorrect, out Double returnlValue)
        {
            
            double sociologicalAffection = 0;
            sum = getSummation(project);
            if (sum == 0) sum = 1;
            try
            {

                var count1 = (from c in cecbContext.SimpleMatrixValues
                              join b in cecbContext.Impacts on c.impt_reference equals b.impt_reference
                              join g in cecbContext.Components on b.impt_component equals g.cmpt_reference
                              where c.proj_reference.Contains(project)
                              && g.cmpt_mainCat.Contains("Social Environmental")

                              group c by new
                              {
                                  c.proj_reference
                              }

                                  into cc
                                  select cc.Sum(c => c.mtrx_value) / sum).FirstOrDefault();

                sociologicalAffection = count1.Value;
                sociologicalAffection = Math.Round(sociologicalAffection, 2);
                isCorrect = true;
                returnlValue = sociologicalAffection;
            }
            catch (Exception)
            {
                isCorrect = false;
                returnlValue = sociologicalAffection;
                 
            }
            
        }

        // calculate the Summation of the total Affection
        public double getSummation(string project) 
        {
             
            try
            {
                var quary = (from c in cecbContext.SimpleMatrixValues

                             where c.proj_reference.Contains(project)
                             
                             group c by new
                             {
                                 c.proj_reference
                             }
                                 into cc
                                 select cc.Sum(c => c.mtrx_value)).FirstOrDefault();
                return   quary.Value;
                
            }
            catch (Exception)
            {
              return  sum = 0.0;
                 

            }
             
        }

        public double getSummationPositive(string project)
        {
            try
            {
                var quary = (from c in cecbContext.SimpleMatrixValues

                             where c.proj_reference.Contains(project)

                             group c by new
                             {
                                 c.proj_reference
                             }
                                 into cc
                                 select cc.Sum(c => c.mtrx_value < 0 ? c.mtrx_value * -1 : c.mtrx_value * 1)).FirstOrDefault();
                
                return quary.Value;

            }
            catch (Exception)
            {
                return sum = 0.0;
            }

        }

        // check the correctivity of the result
        public double CheckCorrectivity(string project)
        {
            
            double result = 0;
            try
            {
                double summation = getSummation(project);
                var quary = (from c in cecbContext.SimpleMatrixValues
                             join b in cecbContext.Impacts on c.impt_reference equals b.impt_reference
                             join g in cecbContext.Components on b.impt_component equals g.cmpt_reference
                             where c.proj_reference.Contains(project)


                             group c by new
                             {
                                 c.proj_reference
                             }

                                 into cc
                                 select cc.Sum(c => c.mtrx_value) / summation).FirstOrDefault();

                result = quary.Value;
            }
            catch (Exception)
            {
                result = 0;
            }
            return result;

        }

        public bool InsertValuesToSimpleMatrix(String impReference, String actReference, String proReference, String impValue , Boolean isUpdate)
        {
            bool isInserted = false;
            try
            {
                if (!isUpdate)
                {

                    cecbData.SimpleMatrixValues simpleMatrixValueObj = new SimpleMatrixValues();
                    simpleMatrixValueObj.proj_reference = proReference;
                    simpleMatrixValueObj.impt_reference = impReference;
                    simpleMatrixValueObj.actv_reference = actReference;
                    simpleMatrixValueObj.mtrx_value = int.Parse(impValue);
                    simpleMatrixValueObj.mtrx_isRelated = true;
                    simpleMatrixValueObj.mtrx_status = true;
                    cecbContext.AddToSimpleMatrixValues(simpleMatrixValueObj);
                    cecbContext.SaveChanges();
                    isInserted = true;
                }
                else {

                    cecbData.SimpleMatrixValues simpleMatrixObj = cecbContext.SimpleMatrixValues.First(i => i.proj_reference.Equals(proReference) && i.impt_reference.Equals(impReference) && i.actv_reference.Equals(actReference));

                    simpleMatrixObj.mtrx_value = int.Parse(impValue);

                    simpleMatrixObj.mtrx_status = true;
                     
                    cecbContext.SaveChanges();
                    isInserted = true;
                
                
                }
            }
            catch (Exception)
            {

                isInserted = false;
            }


            return isInserted;
        }


        public List<Get_Simple_Impacts_Result> GetSimpleImpactsValues(string matrixProjectRef)
        {
            List<Get_Simple_Impacts_Result> Query =
                    (from t in cecbContext.Get_Simple_Impacts(matrixProjectRef)
                     select t).ToList();

            List<Get_Simple_Impacts_Result> results = Query.ToList();

            return results;
        }


        public List<Get_Simple_Phase_Result> GetSimplePhaseValues(string matrixProjectRef)
        {
            List<Get_Simple_Phase_Result> Query =
                    (from t in cecbContext.Get_Simple_Phase(matrixProjectRef)
                     select t).ToList();

            List<Get_Simple_Phase_Result> results = Query.ToList();

            return results;
        }


        public List<Get_Simple_Activities_Result> GetSimpleActivitiesValues(string matrixProjectRef)
        {
            List<Get_Simple_Activities_Result> Query =
                    (from t in cecbContext.Get_Simple_Activities(matrixProjectRef)
                     select t).ToList();

            List<Get_Simple_Activities_Result> results = Query.ToList();

            return results;
        }

       

    }
}
